<template>
  <div class="swiper-container" circular="true">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="carousel in list" :key="carousel.id">
        <img :src="carousel.imgUrl" alt="图片加载失败" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev button"></div>
    <div class="swiper-button-next button"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
export default {
  name: "Carousel",
  props: ["list"],
  watch: {
    list: {
      immediate: true,
      handler(newval, oldval) {
        this.$nextTick(() => {
          var mySwiper = new Swiper(".swiper-container", {
            loop: true, // 循环模式选项
            autoplay: {
              delay: 1500,
              stopOnLastSlide: false,
              disableOnInteraction: true,
            },

            // 如果需要分页器
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
          mySwiper.el.onmouseover = function () {
            mySwiper.autoplay.stop();
          };
          mySwiper.el.onmouseout = function () {
            mySwiper.autoplay.start();
          };
        });
      },
    },
  },
};
</script>

<style lang="css" scoped>
.swiper-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.swiper-container .swiper-slide img {
  width: 100%;
}
.swiper-container .button {
  position: absolute;
  top: 50%;
  margin-top: -17px;
  width: 30px;
  height: 35px;
  background-color: rgba(255, 255, 255, 0.5);
  text-align: center;
  line-height: 35px;
  font-size: 5px;
}
.swiper-container .swiper-button-prev {
  left: 0px;
  border-radius: 0 17px 17px 0;
}
.swiper-container .swiper-button-prev::after {
  content: "\e8d0";
  font-family: "iconfont";
  font-size: 15px;
  color: #333;
}
.swiper-container .swiper-button-next {
  right: 0px;
  border-radius: 17px 0 0 17px;
}
.swiper-container .swiper-button-next::after {
  content: "\e8d2";
  font-family: "iconfont";
  font-size: 15px;
  color: #333;
}
.swiper-container .swiper-pagination {
  position: absolute;
  bottom: 20px;
  left: 30px;
  width: 90px;
  height: 14px;
  --swiper-pagination-color: #fff;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 7px;
  line-height: 14px;
}
</style>
